package com.xinya.wanandroidclient.common

import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import org.jetbrains.annotations.NotNull

/**
 *
 * @ClassName:      ViewExt$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2021/2/24$ 10:47$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
fun ViewPager2.initFragment(
    @NotNull fragment: Fragment,
    @NotNull list: MutableList<Fragment>,
): ViewPager2 {
    adapter = object : FragmentStateAdapter(fragment) {
        override fun getItemCount() = list.size

        override fun createFragment(position: Int) = list[position]
    }
    return this
}

/**
 * 设置禁止固定时间内多次点击
 */
fun View.setNoRepeatClick(interval: Long = 400, click: (View) -> Unit) {
    var lastClickTime: Long = 0L
    setOnClickListener {
        val currentClickTime = System.currentTimeMillis()
        if (currentClickTime - lastClickTime <= interval) {
            Log.i("NoRepeatClick", "setNoRepeatClick: time is to short for click")
            return@setOnClickListener
        }
        lastClickTime = currentClickTime
        click.invoke(it)
    }
}
